<?php
class Menu
{
	private $tableMenu;
	
	public function __construct()
	{
        $this->tableMenu = new Zend_Db_Table('menu');
    }
	

    public function getChildren($ID)
    {
         $Rows = $this->tableMenu->fetchAll( $this->tableMenu->select()->where('PID = ?', $ID)->order('weight'));
         if ($Rows)
         {
            return $Rows->toArray();
         }  
         else
         {
            return array();
         }
    }
    
    
    public function get($PMID)
    {
    	$Row = $this->tableMenu->find($PMID)->current();
        if($Row) return $Row->toArray();
         
        return false;
    }
    
    
    public function add($PMID,$Name)
    {
    	$Row = $this->tableMenu->createRow(array('PID'=>$PMID,'Name'=>$Name));
    	$Row->save();
    	return  $Row->ID;
    }
    
    public function updatelink($PMID,$Link)
    {
        $Row = $this->tableMenu->find($PMID)->current();
        $Row->Link = $Link;
        $Row->save();
    }
    
    
    public function updateFullLink($PMID,$linkParams)
    {
        $Row = $this->tableMenu->find($PMID)->current();
        $Row->setFromArray($linkParams);
        $Row->save();
    }
    
    
    
    public function delete($PMID)
    {
        $Row = $this->tableMenu->find($PMID)->current();
        if($Row)
        $Row->delete();
    }
     
    public function rename($PMID, $Name)
    {
        $Row = $this->tableMenu->find($PMID)->current();
        if($Row)
        {
        	$Row->Name = $Name;
        	$Row->save();
        }
    }
    
     
    public function buildHierarchy()
    {
        $cacheEnabled = Zend_Registry::get('config')->performance->cachingEnabled;
        
    	if ($cacheEnabled)
    	{
            $cache = Zend_Registry::get('cacheGlobal');
            if($result = $cache->load('menu')) {
                return $result; 
            }
    	}
        
        
        $Levels = $this->getChildren(0);
        $Levels = $this->rBuild($Levels);
        
        if ($cacheEnabled)
    	{
            $cache->save($Levels, 'menu');
    	}
        
        return $Levels; 
    } 
    
    
    
    
    
    
    public function formNavHierarchy($Structure)
    {
    	$newStructure = array();
    	foreach ($Structure as $Item)
    	{
    		$newItem = array();
    		$dropTheItem = false;
    		$ignoreSubIems = false;
    		
    		$menuItemType = $Item['Type'];
    		$menuItemParams = unserialize( $Item['Params'] );
    		
    		$newItem['label'] = $Item['Name'];
    		$newItem['order'] = $Item['Weight'];
    		
    		switch ($menuItemType) {
    		    case 'simpleRoutedObject':
        		    $newItem['route']      = $menuItemParams['route'];
        	    	$newItem['module']     = $menuItemParams['module'];
        	    	$newItem['controller'] = $menuItemParams['controller'];
        	    	$newItem['action']     = $menuItemParams['action'];
        	    	
//        	    	$newItem['params']     = array('id'=>$Item['ID'], 'title'=>Transliteration::asURLSegment($Item['Name'])      );
        	    	$newItem['params']     = array_diff_key($menuItemParams, $newItem);

    		    break;
    		    
    		    case 'nestedClassObject':
    		        Loader::includeModuleModels( $menuItemParams['module'] );
    		        $model = new $menuItemParams['class']();
    		        $newItem['pages'] = $model->getMenu($menuItemParams['params']);
    		        $newItem['uri'] = $menuItemParams['uri'];
    		        $ignoreSubIems = true;
    		    break;

    		    
    		    case 'externalLink':
    		        $newItem['uri'] = $menuItemParams['link'];
    		        $newItem['pages'] = array();
    		        $ignoreSubIems = true;
    		    break;
    		    
    		    case 'internalLink':
    		        $newItem['uri'] = $this->baseUrl($menuItemParams['uri']);
    		        $ignoreSubIems = true;
    		    break;
    		    
    		    default:
    		        $dropTheItem = true;
    		        $ignoreSubIems = true;
    		        break;
    		}
    		
    		
    		if (  count($Item['sub']) && (!$ignoreSubIems)  )
    			$newItem['pages'] = $this->formNavHierarchy($Item['sub']);
    		
    			if (!$dropTheItem) 
    			$newStructure[] = $newItem;
    	}
    	return $newStructure;
    }
    	
    private function rBuild(&$Array)
    {
        foreach ($Array as $Key => $Item)
        {
            $Children = $this->getChildren($Item['ID']);
            $Array[$Key]['sub'] = $Children;
            $this->rBuild($Array[$Key]['sub']);
        }
        return $Array;
    } 
    
    
 
	public function ManiMenu()
	{
		$Category = new Category();
		$MainCategories = $Category->getChildren(0); 
		$Layout = Zend_Layout::getMvcInstance();
        $view = $Layout->getView();
        $Front = Zend_Controller_Front::getInstance();
        $Lang = $Front->getRequest()->getParam('lang');
        return $view->partial($Lang . '/menu/main.phtml',array('MC'=>$MainCategories));
	} 
	
	
	public function buildSelect($Level = 0)
	{
		$Hierarchy =$this->buildHierarchy();
        $Layout = Zend_Layout::getMvcInstance();
        $view = $Layout->getView();
        
        $Front = Zend_Controller_Front::getInstance();
        $Lang = $Front->getRequest()->getParam('lang');
        
        if ($Level==0)
        return $view->partial($Lang . '/partial/menu/select.phtml',array('Elements'=>$Hierarchy));
        else
        return $view->partial($Lang . '/partial/menu/selectone.phtml',array('Elements'=>$Hierarchy));
	}

	public function buildULLI()
    {
        $Hierarchy =$this->buildHierarchy();
        $Layout = Zend_Layout::getMvcInstance();
        $view = $Layout->getView();
        
        $Front = Zend_Controller_Front::getInstance();
        $Lang = $Front->getRequest()->getParam('lang');
        
        return $view->partial($Lang . '/partial/menu/ulli.phtml',array('Elements'=>$Hierarchy));
    } 
	
	
}